package com.zmj.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.zmj.framework.base.service.impl.BaseServiceImpl;
import com.zmj.framework.exception.CustomException;
import com.zmj.framework.response.ResponseCode;
import com.zmj.mobile.dto.TechPurDetailBaseDTO;
import com.zmj.mobile.entity.TechPurDetailEntity;
import com.zmj.mobile.mapper.ITechPurDetailMapper;
import com.zmj.mobile.service.ITechPurDetailService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 研发试制/采购申请明细表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2024-02-01
 */
@Service
public class TechPurDetailServiceImpl extends BaseServiceImpl<ITechPurDetailMapper, TechPurDetailEntity> implements ITechPurDetailService {

    /**
     * @Description 导入研发试制主表
     * @Author songxin
     * @Date 18:18 2024/2/1 0001
     **/
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) {

        //加载excel文件
        List<TechPurDetailBaseDTO> TechPurDetailBaseDTOS = EasyExcel.read(file.getInputStream()).excelType(ExcelTypeEnum.XLSX)
                .extraRead(CellExtraTypeEnum.HYPERLINK).extraRead(CellExtraTypeEnum.HYPERLINK)
                .head(TechPurDetailBaseDTO.class)
                .sheet()
                .doReadSync();
        if (CollectionUtil.isEmpty(TechPurDetailBaseDTOS)) {
            throw new CustomException(ResponseCode.INTERNAL_SERVER_ERROR.getCode());
        }
        //
        List<TechPurDetailEntity> techPurDetailEntityList = new ArrayList<>();
        for (TechPurDetailBaseDTO TechPurDetailBaseDTO : TechPurDetailBaseDTOS) {
            TechPurDetailEntity techPurDetailEntity = new TechPurDetailEntity();
            BeanUtil.copyProperties(TechPurDetailBaseDTO, techPurDetailEntity);
            //quoteItemDetailEntity.setQuoteItemId(System.currentTimeMillis());
            techPurDetailEntityList.add(techPurDetailEntity);
        }
        this.saveBatch(techPurDetailEntityList);
    }
}
